package com.lw.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lw.system.Role;
import com.lw.system.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserMapper extends BaseMapper<User> {
    @Select( "select * from users ${ew.customSqlSegment}" )
    @Results({
            @Result(id = true,property = "id",column = "id"),
            @Result(property = "username",column = "username"),
            @Result(property = "password",column = "password"),
            @Result(property = "mobile",column = "mobile"),
            @Result(property = "enableState",column = "enable_state"),
            @Result(property = "createTime",column = "create_time"),
            @Result(property = "companyId",column = "company_id"),
            @Result(property = "companyName",column = "company_name"),
            @Result(property = "departmentId",column = "department_id"),
            @Result(property = "timeOfEntry",column = "time_of_entry"),
            @Result(property = "formOfEmployment",column = "form_of_employment"),
            @Result(property = "workNumber",column = "work_number"),
            @Result(property = "formOfManagement",column = "form_of_management"),
            @Result(property = "workCity",column = "work_city"),
            @Result(property = "correctionTime",column = "correction_time"),
            @Result(property = "inServiceStatus",column = "in_service_status"),
            @Result(property = "departmentName",column = "department_name"),
            @Result(property = "roles",column = "id",javaType = java.util.List.class,many = @Many(select = "com.lw.mapper.RoleMapper.findById"))
    })
    Page<User> findAll(Page page,@Param(Constants.WRAPPER) QueryWrapper<User> wrapper);
    @Select( "select * from role where id not in (select role_id from users_role where user_id=#{id})" )
    List<Role> findOtherRoles(String id);
    @Insert( "insert into users_role(user_id,role_id) values(#{userId},#{roleId})" )
    void addRole(@Param( "userId" ) String userId,@Param( "roleId" ) String id);
    @Select( "select * from users where id=#{id}" )
    @Results({
            @Result(id = true,property = "id",column = "id"),
            @Result(property = "username",column = "username"),
            @Result(property = "password",column = "password"),
            @Result(property = "mobile",column = "mobile"),
            @Result(property = "enableState",column = "enable_state"),
            @Result(property = "createTime",column = "create_time"),
            @Result(property = "companyId",column = "company_id"),
            @Result(property = "companyName",column = "company_name"),
            @Result(property = "departmentId",column = "department_id"),
            @Result(property = "timeOfEntry",column = "time_of_entry"),
            @Result(property = "formOfEmployment",column = "form_of_employment"),
            @Result(property = "workNumber",column = "work_number"),
            @Result(property = "formOfManagement",column = "form_of_management"),
            @Result(property = "workCity",column = "work_city"),
            @Result(property = "correctionTime",column = "correction_time"),
            @Result(property = "inServiceStatus",column = "in_service_status"),
            @Result(property = "departmentName",column = "department_name"),
            @Result(property = "roles",column = "id",javaType = java.util.List.class,many = @Many(select = "com.lw.mapper.RoleMapper.findById"))
    })
    User findById(String id);
}
